package com.sanpu.kxe.business.main.presenter;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.CreditEventOutput;
import com.sanpu.kxe.business.main.contract.CreditEventContract;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.utils.ARouterUtils;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：信用卡活动
 * Created by 薛勇军 on 2019/4/1.
 */

public class CreditEventPresenter implements CreditEventContract.Presenter {

    private CreditEventContract.View mView;

    private CompositeSubscription mSubscription;

    public CreditEventPresenter(CreditEventContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void creditcardActList() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().creditcardActList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CreditEventOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<CreditEventOutput> creditEventOutputs) {
                        mView.showDataView(creditEventOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onItemClick(CreditEventOutput output) {
        if (output != null) {
            switch (output.getSkipType()) {
                case MainConfig.SKIPTYPE_NATIVE:
                    String appNative = output.getAppNative().getAndroid();
                    if (ARouterUtils.containRouter(appNative)) {
                        ARouter.getInstance()
                                .build(appNative)
                                .withTransition(R.anim.in_from_right, R.anim.in_to_left)
                                .navigation();
                        ARouter.getInstance().build(appNative);
                    }
                    break;
                case MainConfig.SKIPTYPE_H5:
                    if (TextUtils.isEmpty(output.getUrl())) return;
                    MainOpenProvider.getInstance().openBrowserX5Activity(
                            mView.getContext(), output.getAdName(), output.getUrl());
                    break;
            }
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
